<System.Runtime.InteropServices.ComVisible(False)> Friend Class gaugeSpeed
    Private m_xoffset As Integer
    Private m_yoffset As Integer = -5
    Private m_txt As String = "km/hr"
    Private m_divisor As Double = 1
    Private m_KmhDisplay As Boolean = True

    Private mGraphicManager As BufferedGraphicsContext = Nothing
    Private mBackgroundBuffer As BufferedGraphics = Nothing

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        DrawPoints(Needle2)
    End Sub

    ''' <summary>
    ''' Takes the value in km/hr 
    ''' </summary>
    ''' <value></value>
    ''' <returns>value in current unit of messure</returns>
    ''' <remarks>auto converts kmhr to mph</remarks>
    Public Property Value() As Double
        Get
            Return Needle2.Value
        End Get
        Set(ByVal value As Double)
            value = (value / m_divisor) \ 1
            If value <= Needle2.Maximum And value >= Needle2.Minimum Then
                If value <> Needle2.Value Then
                    Needle2.Value = value
                    Me.Refresh()
                End If
            End If
        End Set
    End Property
    Public Property Metric() As Boolean
        Get
            Return m_KmhDisplay
        End Get
        Set(ByVal value As Boolean)
            m_KmhDisplay = value
            Select Case value
                Case True
                    m_divisor = 1
                    Needle2.PointsText = "0,50,100,150,200,250"
                    Needle2.Maximum = 250
                    Needle2.Threshold = 20
                    Needle2.Warning = 60
                    Needle2.Minimum = 0
                    Needle2.Value = 0
                    Needle2.text = "Kph"
                    Me.Refresh()
                Case False
                    Needle2.PointsText = "0,32,64,96,128,160"
                    Needle2.Maximum = 160
                    Needle2.Threshold = 12.5
                    Needle2.Warning = 37.5
                    m_divisor = 1.609344
                    Needle2.text = "Mph"
                    Needle2.Value = 0
                    Me.Refresh()
            End Select
        End Set
    End Property
    Public Property subSize() As Double
        Get
            Return Needle2.Length
        End Get
        Set(ByVal value As Double)
            Needle2.Length = value
            Me.Refresh()
        End Set
    End Property
    Public Property offsetX() As Integer
        Get
            Return m_xoffset
        End Get
        Set(ByVal value As Integer)
            m_xoffset = value
            Me.Refresh()
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
        End Set
    End Property
    Public Property offsetY() As Integer
        Get
            Return m_yoffset
        End Get
        Set(ByVal value As Integer)
            m_yoffset = value
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
            Me.Refresh()
        End Set
    End Property

    Private Sub gaugeRPM_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Needle2.Bounds = New Rectangle(m_xoffset, m_yoffset, Me.Width, Me.Height)
        ' DrawPoints()
        DrawPoints(Needle2)
        Me.Refresh()
    End Sub

    Private Sub gaugeSpeed_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        If m_KmhDisplay Then
            Metric = False
        Else
            Metric = True
        End If
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Not IsNothing(mBackgroundBuffer) Then mBackgroundBuffer.Render(e.Graphics)
        MyBase.OnPaint(e)
    End Sub
    Private Sub DrawPoints(ByRef sender As ShapeLibrary.Needle) Handles Needle2.onRedrewPoints
        mGraphicManager = BufferedGraphicsManager.Current
        mGraphicManager.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
        mBackgroundBuffer = mGraphicManager.Allocate(Me.CreateGraphics, Me.ClientRectangle)
        mBackgroundBuffer.Graphics.Clear(Me.BackColor)
        If Not IsNothing(Me.BackgroundImage) Then mBackgroundBuffer.Graphics.DrawImage(Me.BackgroundImage, New PointF(0, 0))
        sender.drawGaugePoints(mBackgroundBuffer.Graphics)
    End Sub

End Class
